package cn.bluarry.donationuser.controller;

import cn.bluarry.donationuser.Respose.UserResponse;
import cn.bluarry.donationuser.mapper.RequestMapper;
import cn.bluarry.donationuser.pojo.Donation_Request;
import cn.bluarry.donationuser.pojo.Donation_User;
import cn.bluarry.donationuser.service.IUserService;
import cn.bluarry.donationuser.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;

@RestController
public class UserController {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private RequestMapper requestMapper;


    @RequestMapping(value = "/user",produces ={ MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
    public Serializable User(HttpServletRequest request){
        String action=request.getParameter("Action");

        String requestid= Utils.getUUID();
        Date currentdate=new Date();
        Donation_Request donation_request=new Donation_Request(requestid,Utils.requesttoString(request),currentdate);
        requestMapper.insert(donation_request);


        if(StringUtils.isBlank(action)){
            return  UserResponse.createByerrorMsg("无Action参数");
        }
        if(action.equals("CreateUser")){
            return iUserService.CreateUser(request,requestid,currentdate);
        }else if( action.equals("ListUsers")){
            return iUserService.ListUsers(request,requestid);
        }else if( action.equals("UpdateUser")){
            return  iUserService.UpdateUser(request,requestid,currentdate);
        }else if( action.equals("DeleteUser")){
            return iUserService.DeleteUser(request,requestid);
        }
        return UserResponse.createByerrorMsg("未知的Action方式");
    }

    @RequestMapping(value="/test")
    public Serializable TestCreateUser(Donation_User donationUser){
        return "hello";
    }


}
